
/**   
*    
* ��Ŀ��ƣ�a_yongan   
* ����ƣ�UserController   
* ��������   
* �����ˣ�liubo   
* ����ʱ�䣺2018��5��30�� ����10:31:44    
* �޸�ʱ�䣺2018��5��30�� ����10:31:44   
* �޸ı�ע��   
* @version    
*    
*/
package com.yongan.user.web;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yongan.user.entity.User;
import com.yongan.user.service.UserService;
import com.yongan.user.util.VCodeGenerator;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/")
public class HomeController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserService userService;
	
	/**
     * ����ҳ/��¼ҳ��
     */
	@RequestMapping(value={"", "/", "/login"},method=RequestMethod.GET)
	public String index(Model model){
		return "login";
	}
	
	@RequestMapping(value={"/index"},method=RequestMethod.GET)
	public String indexin(Model model){
		return "index";
	}
	
	@RequestMapping(value="maincontent",method=RequestMethod.GET)
	public String maincontent(Model model){
		return "maincontent";
	}
	/*TODO*/
	@RequestMapping(value="dologin",method=RequestMethod.POST)
	public String login(Model model, HttpServletRequest request,HttpSession session){
		//System.out.println(request.getParameter("userName"));
	    String userName = null;
		try {
			userName = new String(request.getParameter("userName").getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}  
		//String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String vcode = request.getParameter("vcode");
		if(userName.isEmpty()){
            session.setAttribute("errorMsg", "用户名为空!");
            return "redirect:/";
        }
        if(password.isEmpty()){
            session.setAttribute("errorMsg", "密码为空!");
            return "redirect:/";
        }
        if(vcode.isEmpty()){
            session.setAttribute("errorMsg", "验证码为空!");
            return "redirect:/";
        }
        //��֤��
        String sessionVcode = (String) session.getAttribute("vcode");
        if(!vcode.equalsIgnoreCase(sessionVcode)){
            session.setAttribute("errorMsg", "验证码错误!");
            return "redirect:/";
        }
        //System.out.println("="+userName+"============"+password+"========="+vcode+"==============");
		User user = userService.checkUser(userName, password);
		User userid = userService.checkUserById(userName, password);
		if(user==null & userid==null){
			session.setAttribute("errorMsg", "无此用户!");
			return "redirect:/";
		}else{
			if(user==null){
				session.setAttribute("username",userid.getUserName());
				session.setAttribute("userid",userid.getUserId());
			}else{
				session.setAttribute("username",user.getUserName());
				session.setAttribute("userid",user.getUserId());
			}
			return "redirect:/index";
		}
	}
	
	/**
     * �˳�ϵͳ
     */
    @RequestMapping("/logoff")
    public String logoff(HttpSession session){
        session.invalidate();
        return "redirect:/index";
    }
    
	  /**
     * ��ȡ��֤��
     */
    @RequestMapping(value="vcode",method=RequestMethod.GET)
    public void getVCode(HttpSession session, HttpServletResponse response) throws IOException {
        //������֤�����������
        VCodeGenerator vcGenerator = new VCodeGenerator();
        //�����֤��
        String vcode = vcGenerator.generatorVCode();
        //����֤�뱣����session����,�Ա��ж���֤���Ƿ���ȷ
        session.setAttribute("vcode", vcode);
        //�����֤��ͼƬ
        BufferedImage vImg = vcGenerator.generatorRotateVCodeImage(vcode, true);
        //���ͼ��
        ImageIO.write(vImg, "gif", response.getOutputStream());
    }
}
